﻿using System.Xml;
using System.Xml.Schema;
using System.ServiceModel.Description;
using System.Collections.Generic;
using System;

namespace Tipler.CompressionEncoder
{
    public class CompressionMessageEncodingBindingElementImporter : IPolicyImportExtension
    {
        void IPolicyImportExtension.ImportPolicy(MetadataImporter importer, PolicyConversionContext context)
        {

            if ((importer == null))
            {
                throw new ArgumentNullException("importer");
            }
            if ((context == null))
            {
                throw new ArgumentNullException("context");
            }
            ICollection<XmlElement> assertions = context.GetBindingAssertions();
            foreach (XmlElement assertion in assertions)
            {
                if (((object.ReferenceEquals(assertion.NamespaceURI, "http://schemas.microsoft.com/ws/06/2004/mspolicy/netgzip1")) && (object.ReferenceEquals(assertion.LocalName, "GZipEncoding"))))
                {
                    assertions.Remove(assertion);
                    context.BindingElements.Add(new CompressionMessageEncodingBindingElement());
                    return;
                }
            }
        }

    }
}
